﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace BaoJian.e
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void FinsihedReg_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.Cookies["CheckCode"] == null)
            {
                Msg.Text = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
                Msg.Visible = true;
                return;
            }
            

            if (String.Compare(Request.Cookies["CheckCode"].Value, verifyCode.Value.ToString().Trim(), true) != 0)
            {
                Msg.Text = "<font color=red>对不起，验证码错误！</font>";
                Msg.Visible = true;
                return;
            }

            if (Membership.FindUsersByEmail(userName.Value).Count > 0)
            {
                Msg.Text = "重复的邮箱，请重新选择";
                Msg.Visible = true;
                return ;
            }

            if (CreateUser())
            {
                CreateAuthenticationTicket(userName.Value, false);
                HttpContext.Current.Response.Redirect("yourAccountApp.aspx");
            }

        }

        private bool CreateUser()
        {
            bool returnValue = false;
            MembershipCreateStatus status;
            Membership.CreateUser(userName.Value, passWord.Value, userName.Value, nickName.Value, nickName.Value, true, out status);         

           switch (status)
           {
               case MembershipCreateStatus.Success:
                   Roles.AddUserToRole(userName.Value, "member");
                   returnValue = true;
                   break;
              case MembershipCreateStatus.DuplicateUserName:
                   Msg.Text = "该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.DuplicateEmail:
                  Msg.Text = "该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.InvalidPassword:
                  Msg.Text = "密码无效，请输入有效的密码"; break;
              case MembershipCreateStatus.InvalidEmail:
                  Msg.Text = "该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.InvalidAnswer:
                  Msg.Text = "该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.InvalidQuestion:
                  Msg.Text = "T该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.InvalidUserName:
                  Msg.Text = "该邮箱已经存在,请重新选择"; break;
              case MembershipCreateStatus.ProviderError:
                  Msg.Text = "授权失败，请联系管理员"; break;
              case MembershipCreateStatus.UserRejected:
                  Msg.Text = "创建用户失败，请联系管理员"; break;              
              default:
                  Msg.Text = "未知错误，请重试。如果反复出现，请联系管理员"; break;
           }

          //MembershipUser user = Membership.GetUser(userName.Value);

           LYQ.BLL.Users bll = new LYQ.BLL.Users();
           LYQ.Model.Users model = new LYQ.Model.Users();
           model.UserID = userName.Value;
           model.UserName = nickName.Value;
           model.Birthday = DateTime.Parse("01/01/1930 00:00:00");
           model.Address = string.Empty;
           model.Tel = string.Empty;
           model.Mobile = string.Empty;
           model.RegisterTime = DateTime.Now;
           model.EMail = userName.Value;
           model.BonusPoints = 0;
           model.Password = string.Empty;
           model.Sex = string.Empty;
           model.Province = string.Empty;
           model.LoginCount = 0;
           bll.Add(model);   

           return returnValue;
        }

        private bool CreateAuthenticationTicket(string username, bool isPersistent)
        {
            int version = 1;
            DateTime issueDate = DateTime.Now;
            DateTime expiration;
            string cookiePath = "/";
            string[] userData = Roles.GetRolesForUser(username);
            if (isPersistent)
                expiration = DateTime.Now.AddYears(1);
            else
                expiration = DateTime.Now.AddDays(1);

            FormsAuthenticationTicket FormAuthTicket = new
                FormsAuthenticationTicket(version, username, issueDate, expiration,isPersistent, userData[0], cookiePath);
            string encTicket = FormsAuthentication.Encrypt(FormAuthTicket);

            HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            Response.Cookies.Add(AuthCookie);
            return true;

        }

       
    }
}
